package com.badibadi.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.PopupWindow;

import com.badibadi.adapter.ClubMyClubListAdapter;
import com.badibadi.adapter.ClubOverviewZuixinAdapter_1;
import com.badibadi.infos.ClubModel;
import com.badibadi.infos.ClubMy_Model;
import com.badibadi.infos.Results;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.DisplayUtil;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.miloisbadboy.view.PullToRefreshView;
import com.miloisbadboy.view.PullToRefreshView.OnFooterRefreshListener;
import com.miloisbadboy.view.PullToRefreshView.OnHeaderRefreshListener;

public class MyClubFragment1 extends BaseFragment implements
		OnHeaderRefreshListener, OnFooterRefreshListener {
	String TAG = "MyClubFragment0";
	static MyClubFragment1 myClubFragment1;
	private GridView gv;
	private PullToRefreshView mPullToRefreshView;
	private int Page;
	private List<ClubMy_Model> clubModels;
	private List<ClubMy_Model> list;// 临时存放数据的
	private String type = "0";// 0最新 1最火 2自创
	private ClubMyClubListAdapter clubMyClubListAdapter;
	private String uid = "26";// 用户id
	private String fid = "26";// 朋友id
	private int pageNum = 10;// 每页数量

	PopupWindow popup;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case 1:
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(), getResources()
							.getString(R.string.l_net_error));
					mPullToRefreshView.onFooterRefreshComplete();
					mPullToRefreshView.onHeaderRefreshComplete();
					break;
				case 2:
					Utils.ExitPrgress(getActivity());
					clubModels.addAll(list);
					clubMyClubListAdapter.notifyDataSetChanged();
					mPullToRefreshView.onFooterRefreshComplete();
					mPullToRefreshView.onHeaderRefreshComplete();
					break;
				case 3:
					Utils.ExitPrgress(getActivity());
					Utils.showMessage(getActivity(), getResources()
							.getString(R.string.l_xa10));
					mPullToRefreshView.onFooterRefreshComplete();
					mPullToRefreshView.onHeaderRefreshComplete();
					break;
				default:
					break;
				}
			} catch (Exception e) {
				   
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		clubModels = new ArrayList<ClubMy_Model>();
		uid = getArguments().getString("uid", "-1");
		type = getArguments().getString("type");
		clubMyClubListAdapter = new ClubMyClubListAdapter(getActivity(),
				clubModels);

	}// regionhomepage_import_layout2

	@Override
	public View onCreateView(final LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		final View view = inflater.inflate(R.layout.xgridview_1, null);
		Page = 1;
		mPullToRefreshView = (PullToRefreshView) view
				.findViewById(R.id.main_pull_refresh_view);
		gv = (GridView) view.findViewById(R.id.xgridView11);
		// gv.setXGridViewListener(this);
		gv.setNumColumns(2);
		gv.setPadding(0, DisplayUtil.dip2px(getActivity(), 10),
				DisplayUtil.dip2px(getActivity(), 10),
				DisplayUtil.dip2px(getActivity(), 10));
		gv.setAdapter(clubMyClubListAdapter);
		LoadingInterNet(Page);
		mPullToRefreshView.setOnHeaderRefreshListener(this);
		mPullToRefreshView.setOnFooterRefreshListener(this);

		return view;
	}

	private void LoadingInterNet(final int page) {
//		   
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", uid);
//				   
				if (fid.equals("-1") != true) {
					map.put("fid", fid);
				}
				map.put("type", type);
				map.put("page", page);
				map.put("pageNum", pageNum);
				String invite_content = Utils.sendRequest(map,
						Constants.BadiUrl + "club");
				//    
				if (invite_content != null) {
					Results results = Utils.checkResult_NNN(getActivity(),
							invite_content);
					if (results != null && !results.getRetmsg().equals("null")) {
//						   
						try {
							list = JSONUtils.getListByJsonString(
									results.getRetmsg(), ClubMy_Model.class);
							// mList.addAll(list);
							mHandler.sendEmptyMessage(2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						if (page > 1) {
							--Page;
						} else {
							Page = 1;
						}
						mHandler.sendEmptyMessage(3);
					}
				} else {
					if (page > 1) {
						--Page;
					} else {
						Page = 1;
					}
					mHandler.sendEmptyMessage(1);
				}

			}
		});
	}

	// private void Show_club(final int page) {
	// // Utils.showPrgress(getActivity());
	// MyThreadTool.fixedThreadPool.execute(new Runnable() {
	// @Override
	// public void run() {
	// List<ClubModel> clubs = new ArrayList<ClubModel>();
	// Map<String, Object> map = new HashMap<String, Object>();
	// map.put("type", type);
	// map.put("page", page);
	// map.put("pageNum", 6);
	//    (map.toString());
	// String content = Utils.sendRequest(map,
	// com.badibadi.mytools.Constants.BadiUrl + "show_club");
	// if (content != null) {
	// Results results = Utils.checkResult_NNN(getActivity(),
	// content);
	// if (results != null && !results.getRetmsg().equals("null")) {
	//    ("结果是:" + results.getRetmsg());
	// try {
	// clubs = JSONUtils.getListByJsonString(
	// results.getRetmsg(), ClubModel.class);
	// clubModels.addAll(clubs);
	// handler.sendEmptyMessage(2);
	// } catch (JSONException e) {
	// // TODO Auto-generated catch block
	//    
	// }
	// } else {
	// if (Page > 1) {
	// --Page;
	// } else {
	// Page = 1;
	// }
	// handler.sendEmptyMessage(3);
	// }
	// } else {
	// if (Page > 1) {
	// --Page;
	// } else {
	// Page = 1;
	// }
	// handler.sendEmptyMessage(1);
	// }
	// }
	// });
	// }
	public static MyClubFragment1 getInstance() {
		if (myClubFragment1 == null) {
			return new MyClubFragment1();
		}
		return myClubFragment1;
	}

	/**
	 * 打开或管理悬浮对话框
	 * 
	 * @param inflater
	 * @param v
	 */
	public void OpenOrClosePopWindows(LayoutInflater inflater, View v) {
		if (popup == null) {
			View root = inflater.inflate(
					R.layout.fragment_club_overview_popwindows, null);
			popup = new PopupWindow(root, 300, 360);
			popup.showAsDropDown(v);
		} else {
			popup.dismiss();
			popup = null;
		}
	}

	// @Override
	// public void OnLoadMore() {
	// //    
	//
	// }

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		// Page = Page + 1;
		//    
		LoadingInterNet(++Page);
	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		// TODO Auto-generated method stub
		Page = 1;
		if (clubModels != null) {
			clubModels.clear();
		}
		LoadingInterNet(Page);
	}
}
